function twitterHub() {
	
}

twitterHub.prototype.setNeedle = function(newNeedle) {
	this.settings.needle = newNeedle;
	this.settings.needleRegExp = new RegExp(newNeedle,"gi");	
}

twitterHub.prototype.settings = {
	needle:"",
	needleRegExp:"",
	maxTweets:4,
	queryOptions:"",
	maxLibraryItems:"80"
}

twitterHub.prototype.init = function(targetDiv) {
	//Controller
	if (typeof(twitterHubController)!="object") {
		twitterHubController = {
			timeout:"",
			targetDiv:targetDiv,
			settings:this.settings,
			init:function() {
				
				twitterHubDB.loadTweets();
				//put actions on buttons
				
				$("#tweetsHub-button-archive").unbind().click(function(){
					twitterHubController.showArchive();
				});
				
			   
			   $("#tweetsHub-button-refresh")
				.unbind()
				.data("needle",twitterHubController.settings.needle).one("customReload", function() {
					var needle = $(this).data("needle");
					//needleTracer = needle;
					twitterHubController.init();
				}).one("click", function() {
					$(this).trigger("customReload")
					clearTimeout(twitterHubController.timeout);
				});
				
				twitterHubController.timeout = setTimeout(function(){$("#tweetsHub-button-refresh").trigger("customReload")},60000);
				
				if(twitterHubDB.library.length>this.settings.maxLibraryItems) {
					this.cleanList(this.settings.maxLibraryItems);
				}
				
				
			},
			cleanList:function(maxcount) {
				twitterHubDB.cleanList(maxcount);
				
			},
			showArchive:function() {
				window.open("http://search.twitter.com/search?q="+escape(twitterHubController.settings.needle)+twitterHubController.settings.queryOptions);
			}
		}
	}
	
	//View 
	if (typeof(twitterHubView)!="object") {
		twitterHubView = {
			latestView:function() {
				$("#"+twitterHubController.targetDiv).html("").hide();
				
				for (var i=0; i < twitterHubDB.library.length; i++) {
					
					//Set up each of the values
					var tweet = twitterHubDB.library[i].text.replace(twitterHubController.settings.needleRegExp, "").replace(/^\s*/, "").replace(/\s*$/, "");;
					var tweeter = twitterHubDB.library[i].from_user;
					var twitTime = relative_time(twitterHubDB.library[i].created_at);
					var main = (i==0)?"Latest":"Default";
					var main2 = (i==0)?"font-size:13pt; margin-bottom:6px;":"margin-bottom:6px;";
					
					//Append values and formatting to target div
					$("#"+twitterHubController.targetDiv).append('<div class="tweet">'+
																 '<div class="tweet'+main+'" style="'+main2+'">'+tweet+'</div>'+
																 '<div style="font-size:7pt;"><span class="twitterUser">'+tweeter+'</span><span class="twitterTime"> - '+twitTime+'</span></div>'+
																 '</div>');
					
					//Check to see if we overflowed
					var tooTall = $("#"+twitterHubController.targetDiv).height();
					var tallLimit = 290-(((i+1)*6)+(i*12));
					if(parseInt(tooTall) > tallLimit) {
						$(".tweet:last").remove();
						$("#"+twitterHubController.targetDiv).fadeIn();
						break;
					}
					
				}				
			},
			runningView:function() {
				$("#"+twitterHubController.targetDiv).html("").hide();
				
				for (var i=0; i < twitterHubDB.library.length; i++) {
					
					//Set up each of the values
					var tweet = twitterHubDB.library[i].text.replace(twitterHubController.settings.needleRegExp, "").replace(/^\s*/, "").replace(/\s*$/, "");;
					var tweeter = twitterHubDB.library[i].from_user;
					var twitTime = relative_time(twitterHubDB.library[i].created_at);
					var main = (i==0)?"Latest":"Default";
					var main2 = (i==0)?"font-size:13pt; margin-bottom:6px;":"margin-bottom:6px;";
					
					//Append values and formatting to target div
					$("#"+twitterHubController.targetDiv).append('<div class="tweet">'+
																 '<div class="tweet'+main+'" style="'+main2+'">'+tweet+'</div>'+
																 '<div style="font-size:7pt;"><span class="twitterUser">'+tweeter+'</span><span class="twitterTime"> - '+twitTime+'</span></div>'+
																 '</div>');
					
				}
				
				$("#"+twitterHubController.targetDiv).show();
			}
		}
	}
	
	//Model
	if (typeof(twitterHubDB)!="object") {
		twitterHubDB = {
			library:new Array(),
			lastRefresh:"",
			loadTweets:function() {
				$.ajax({
					type:"GET",
					url:"http://search.twitter.com/search.json",
					dataType:"jsonp",
					data:"q="+escape(twitterHubController.settings.needle)+twitterHubController.settings.queryOptions+"&since_id="+this.lastRefresh,
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('<p></p>')
						.html("There was an error with your request")
						.appendTo('body');
						returnVal = false;
					},
					//jsonp: "",
					success: function(html) {
						var twitterQueryData = eval(html);
						if (twitterQueryData.results.length >0) {
							twitterHubDB.lastRefresh = twitterQueryData.max_id;
							
							for (var i=twitterQueryData.results.length-1; i>=0; i--) {
								ARArrayTracer = twitterHubDB.library.unshift(twitterQueryData.results[i]);	
								
							}
						}
						
					},
					complete: function() {
						twitterHubView.runningView();	
					}
				});	
				
			},
			cleanList:function(maxcount) {
				var howmany = (this.library.length-maxcount);
				//console.log("running clean list. howmany:"+howmany+" maxcount:"+maxcount);
				this.library.splice(maxcount,howmany);
				
			}
			
		}
	}
	
	twitterHubController.init();
	
}